// pages/logs/log.js
const util = require('../../utils/util.js')
const {
  request
} = require("../../utils/index.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '', //用户名
    Information: '待完善', //资料完善状态
    realname: '待认证', //实名状态
    userimg: '', //用户头像
    userInfo: "", //用户信息
    loginzt: false, //登录状态
    mess: 0, //消息
    textcolor: "red",  //认证
    textcolor2:"red",  //资料
  },

  // 获得用户信息
  async userzl(e) {
    try {
      let token = wx.getStorageSync('token')
      let par = {
        url: "hw/portal/user/self",
        method: "GET",
        data: {
          token: token
        },
        header: {
          'content-type': 'application/json', // 默认值
        },
      }
      let datainfo = await request(par);
      wx.setStorageSync("userinfo", datainfo.data);
      if (datainfo.status == 100) {
        this.setData({
          userInfo: datainfo.data,
        })
      }
      //认证状态
      if (this.data.userInfo.auditStatus != "" && this.data.userInfo.auditStatus != undefined) {
        this.setData({
          realname: "认证" + this.data.userInfo.auditStatus.desc
        })
      }
      if(this.data.realname!="待认证"){
        this.setData({
          textcolor:"#fe8d32"
        })
      }
      //若不通过则改变认证状态颜色
      console.log(this.data.realname)
      if(this.data.userInfo.auditStatus!=undefined){
        if(this.data.userInfo.auditStatus.code=='fail'){
          this.setData({
            textcolor:"red"
          })
        }else{
          this.setData({
            textcolor:"#fe8d32"
          })
        }
      }
      if(this.data.userInfo.isInfoComplete==true){
        this.setData({
          textcolor2:"#fe8d32"
        })
      }
    } catch (error) {
      console.log(error);
    }
  },

  //用户登录
  handlelogin(e) {
    var that = this;
    wx.login({
      success: async function (res) {
        if (res.code) {
          let params = {
            url: "hw/portal/user/wechat/login",
            method: "POST",
            data: {
              wxCode: res.code
            }
          }
          let result = await request(params);
          if (result.status == 100) {
            wx.setStorageSync('token', result.data.token)
            wx.setStorageSync('userId', result.data.userId)
            that.setData({
              loginzt: true,
              userName: result.data.userName,
              userimg: result.data.portrait,
              token: wx.getStorageSync('token'),
              mess: result.data.msgUnReadCount
            })
            that.userzl();
          }
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })

  },
  leave: function (e) {
    wx.navigateTo({
      url: '../leave/leave',
    })
  },
  AboutUs() {
    wx.navigateTo({
      url: '../AboutUs/AboutUs',
    })
  },


  share: function (potions) {
    if (wx.getStorageSync('userinfo') != undefined) {
      wx.navigateTo({
        url: '../perfect/perfect',
      })
    } else {
      wx.showModal({
        title: '请先登录',
        confirmText: "确认",
        showCancel: false,
      })
    }
  },
  Pers: function (potions) {
    if (wx.getStorageSync('userinfo') != undefined) {
      wx.navigateTo({
        url: '../Perslogs/Perslogs',
      })
    } else {
      wx.showModal({
        title: '请先登录',
        confirmText: "确认",
        showCancel: false,
      })
    }
  },
  release: function (potion) {
    if (wx.getStorageSync('userinfo') != undefined) {
      wx.navigateTo({
        url: '../logsrelease/logsrelease',
      })
    } else {
      wx.showModal({
        title: '请先登录',
        confirmText: "确认",
        showCancel: false,
      })
    }
  },
  collect() {
    wx.navigateTo({
      url: '../Mecollect/Mecollect',
    })
  },
  real: function (potion) {
    if (wx.getStorageSync('userinfo') != undefined) {
      wx.navigateTo({
        url: '../realname/real',
      })
    } else {
      wx.showModal({
        title: '请先登录',
        confirmText: "确认",
        showCancel: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取头像
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    //消息99+
    if (parseInt(this.data.mess) > 99) {
      this.setData({
        mess: "99+"
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //code
    wx.login({
      success(res) {
        console.log(res.code)
      }
    })
    this.handlelogin();
    console.log(this.data.userInfo)
    let token = wx.getStorageSync('token'); //用户token
    let datainfo = wx.getStorageSync("userinfo"); //获得用户信息
    if (this.data.loginzt == true) {} else {
      wx.clearStorageSync()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})